package com.ruoyi.topic.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.request.PageRequest;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.topic.domain.entity.Topic;
import com.ruoyi.topic.service.ITopicPostService;
import com.ruoyi.topic.service.ITopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import static com.ruoyi.common.core.web.domain.AjaxResult.success;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xyf
 * @since 2024-12-18
 */
@RestController
@RequestMapping("/topic")
public class TopicController {
    
    @Autowired
    private ITopicService topicService;


    /**
     * 查询议题列表
     */
    @RequiresPermissions("topic:topic:list")
    @GetMapping("/list")
    public TableDataInfo list(PageRequest pageRequest) {
        Page<Topic> page = topicService.page(new Page<>(pageRequest.getPageNum(), pageRequest.getPageSize()));
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setRows(page.getRecords());
        tableDataInfo.setTotal(page.getTotal());
        return tableDataInfo;
    }

    /**
     * 导出议题列表
     */
    @RequiresPermissions("topic:topic:export")
    @Log(title = "议题", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PageRequest pageRequest)
    {
        Page<Topic> page = topicService.page(new Page<>(pageRequest.getPageNum(), pageRequest.getPageSize()));
        List<Topic> list = page.getRecords();
        ExcelUtil<Topic> util = new ExcelUtil<Topic>(Topic.class);
        util.exportExcel(response, list, "议题数据");
    }

    /**
     * 获取议题详细信息
     */
    @RequiresPermissions("topic:topic:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        Topic Topic = topicService.getById(id);
        return success(Topic);
    }

    /**
     * 新增议题
     */
    @RequiresPermissions("topic:topic:add")
    @Log(title = "议题", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Topic Topic) {
        Topic.setCreateTime(new Date());
        boolean isSuccess = topicService.save(Topic);
        return isSuccess ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 修改议题
     */
    @RequiresPermissions("topic:topic:edit")
    @Log(title = "议题", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Topic Topic) {
        boolean isSuccess = topicService.updateById(Topic);
        return isSuccess ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 删除议题
     */
    @RequiresPermissions("topic:topic:remove")
    @Log(title = "议题", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        List<Long> idList = Arrays.asList(ids);
        boolean isSuccess = topicService.removeBatchByIds(idList);
        return isSuccess ? AjaxResult.success() : AjaxResult.error();
    }

}
